package com.museum.guide.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.guide.entity.Feedback;
import com.museum.guide.mapper.FeedbackMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Service
public class FeedbackService extends ServiceImpl<FeedbackMapper, Feedback> {

    /**
     * 分页查询反馈信息
     */
    public Page<Feedback> page(Page<Feedback> page, String title, String type, String status, Long userId) {
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(title), Feedback::getTitle, title)
                .eq(StringUtils.hasText(type), Feedback::getType, type)
                .eq(StringUtils.hasText(status), Feedback::getStatus, status)
                .eq(userId != null, Feedback::getUserId, userId)
                .orderByDesc(Feedback::getCreateTime);
        return page(page, wrapper);
    }

    /**
     * 新增反馈
     */
    public void saveFeedback(Feedback feedback) {
        feedback.setStatus("PENDING");
        feedback.setCreateTime(LocalDateTime.now());
        feedback.setUpdateTime(LocalDateTime.now());
        save(feedback);
    }

    /**
     * 更新反馈信息
     */
    public void updateFeedback(Feedback feedback) {
        feedback.setUpdateTime(LocalDateTime.now());
        updateById(feedback);
    }

    /**
     * 删除反馈
     */
    public void removeFeedback(Long id) {
        removeById(id);
    }

    /**
     * 回复反馈
     */
    public void replyFeedback(Long id, String reply, Long replyUserId) {
        Feedback feedback = getById(id);
        if (feedback != null) {
            feedback.setReply(reply);
            feedback.setReplyUserId(replyUserId);
            feedback.setReplyTime(LocalDateTime.now());
            feedback.setStatus("RESOLVED");
            feedback.setUpdateTime(LocalDateTime.now());
            updateById(feedback);
        }
    }
}